/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.utils.manager.mock;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MockTransport
    extends Transport
{

  private static final Logger LOGGER = LoggerFactory.getLogger(MockTransport.class);

  public static final Map<String, List<Message>> MESSAGES = new HashMap<String, List<Message>>();



  public MockTransport(Session session, URLName urlName)
  {
    super(session, urlName);
  }



  @Override
  public void connect()
      throws MessagingException
  {
    LOGGER.info("Connecting to MockTransport:connect()");
  }



  @Override
  public void connect(String host, int port, String username, String password)
      throws MessagingException
  {
    LOGGER.info("Connecting to MockTransport:connect(String " + host + ", int " + port + ", String " + username + ", String " + password + ")");
  }



  @Override
  public void connect(String host, String username, String password)
      throws MessagingException
  {
    LOGGER.info("Connecting to MockTransport:connect(String " + host + ", String " + username + ", String " + password + ")");
  }



  @Override
  public void sendMessage(Message message, Address[] addresses)
      throws MessagingException
  {
    LOGGER.info("Sending message '" + message.getSubject() + "'");
    for (Address address : addresses) {
      List<Message> messages = MESSAGES.get(address.toString());
      if (messages == null) {
        messages = new ArrayList<Message>();
        MESSAGES.put(address.toString(), messages);
      }
      messages.add(message);
    }
  }



  @Override
  public void close()
  {
    LOGGER.info("Closing MockTransport:close()");
  }
}
